package com.tgh.gles2.c4.move;

import com.tgh.utils.GLShaderManager;
import com.tgh.utils.GLShaderManager.ShaderType;
import com.tgh.utils.GLTools;

import android.content.Context;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.Matrix;
import android.os.SystemClock;
import android.util.AttributeSet;

import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class MySurfaceView extends GLSurfaceView {

    public MySurfaceView(Context context) {
        super(context);
        init();
    }

    public MySurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    
    public void up() {
        y+=step;
        if (y>1.0f-blockSize) {
            y=1.0f-blockSize;
        }
        requestRender();
    }
    public void down() {
        y-=step;
        if (y<-1.0f+blockSize) {
            y=-1.0f+blockSize;
        }
        requestRender();
    }
    public void left() {
        x-=step;
        if (x<-ratio+blockSize) {
            x=-ratio+blockSize;
        }
        requestRender();
    }
    public void right() {
        x+=step;
        if (x>ratio-blockSize) {
           x=ratio-blockSize;
        }
        requestRender();
    }
    
    private void init() {
        setEGLContextClientVersion(2);
        setRenderer(new SceneRenderer());
        setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
    }
    
    private float x,y;
    private float step=0.025f;
    private float blockSize=0.1f;
    private float ratio;
    
    private class SceneRenderer implements Renderer{
        
        private GLShaderManager shaderManager;
        private FloatBuffer vertexBuffer;
        private float[] transMatrix=new float[16];
        private float[] projectMatrix=new float[16];
        private float[] mvpMatrix=new float[16];
        private float radian=0f;
        private float[] color={0.8f,0.4f,0.125f,1f};
        
        @Override
        public void onSurfaceCreated(GL10 gl, EGLConfig config) {
            shaderManager = new GLShaderManager();
            shaderManager.initStockShader();
            
            float[] vPoints={
                    -blockSize,-blockSize,
                    blockSize,-blockSize,
                    blockSize,blockSize,
                    -blockSize,blockSize
            };
            vertexBuffer = GLTools.makeFloatBuffer(vPoints);
        }

        @Override
        public void onSurfaceChanged(GL10 gl, int width, int height) {
            GLES20.glViewport(0, 0, width, height);
            Matrix.setIdentityM(transMatrix, 0);
            ratio=(float)width/(float)height;
            Matrix.orthoM(projectMatrix, 0, -ratio, ratio, -1, 1, -1, 1);
        }

        @Override
        public void onDrawFrame(GL10 gl) {
            GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
            
            Matrix.setIdentityM(transMatrix, 0);
            Matrix.translateM(transMatrix, 0, x, y, 0);
            
           addRadianAngle();
           
            Matrix.rotateM(transMatrix, 0, radian, 0, 0, 1);
            Matrix.multiplyMM(mvpMatrix, 0, projectMatrix , 0, transMatrix, 0);
            shaderManager.useStockShader(ShaderType.FLAT, mvpMatrix, color);
            GLES20.glVertexAttribPointer(GLShaderManager.GL_ATTRIBUTE_VERTEX, 2, GLES20.GL_FLOAT, false, 0, vertexBuffer);
            GLES20.glEnableVertexAttribArray(GLShaderManager.GL_ATTRIBUTE_VERTEX);
            GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 4);
        }
        
        private void addRadianAngle() {
            radian=SystemClock.elapsedRealtime()/1000*30;
        }
    }
}
